/**
 * 单位行业树
 * @type {Object}
 */
var businessType = new Object();

//初始化
businessType.init = function () {
    businessType.initTree();
}

businessType.tree_options = {
    id: 'tree_business',
    url: appPath + '/super/business/companyBusiness_tree',
    setting: {
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType :  { "Y" : "", "N" : "" }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
      view: {
        dblClickExpand: false,
        fontCss: getFontCss
      },
      callback: {
        onCheck: function (e,treeId, treeNode) {
            var nodes = businessType.business_zTree.getCheckedNodes(true);
            var data = []//,names = [];
            for(var i = 0; i < nodes.length; i++){
                var n = nodes[i];
                if(n.pId == null){
                    data = [];
                    data.push(n.id);
                    if(nodes.length > 1){
                        recursion.cancelNode(businessType.business_zTree, n);
                    }
                    return;
                }
                //names.push(n.name);
                data.push(n.id);
            }
            //解析参数
            var rsBusiness = [];
            if(data != null && data.length > 0){
                for(var i = 0; i < data.length; i++){
                    rsBusiness.push(recursion.getAllChildren(businessType.business_zTree, data[i]));
                }
            }
            $("#businessId").val(rsBusiness.join(","));
           // $("#company_business").val(names);
            $("#tree_business>li>span.chk").hide();// 隐藏root节点checkbox
        }
      }
    }
};

//初始化树
businessType.initTree = function(){
    $.ajax({
      url: businessType.tree_options.url,
      async: false,
      type: 'post',
      data: {},
      success: function(data){
          businessType.tree_options.zNodes = eval(data.result);
          // 加载树
          businessType.business_zTree = $.fn.zTree.init($("#tree_business"), businessType.tree_options.setting, businessType.tree_options.zNodes);
          $("#tree_business>li>span.chk").hide();
          //$("#tree_business>li>span.chk").setChkDisabled();// 隐藏root节点checkbox
      }
    });
};

/**
 * 单位类型树
 * @type {Object}
 */
var categoryType = new Object();

//初始化
categoryType.init = function () {
  categoryType.initTree();
}

categoryType.tree_options = {
      id: 'tree_category',
      url: appPath + '/super/category/companyCategory_tree',
      setting: {
          check: {
              enable: true,
              chkStyle:"checkbox",
              chkboxType :  { "Y" : "", "N" : "" }
          },
          data: {
              simpleData: {
                  enable: true
              }
          },
        view: {
          dblClickExpand: false,
          fontCss: getFontCss
        },
        callback: {
            onCheck: function (e,treeId, treeNode) {
                var nodes = categoryType.category_zTree.getCheckedNodes(true);
                var data = []//,names = [];
                for(var i = 0; i < nodes.length; i++){
                    var n = nodes[i];
                    if(n.pId == null){
                        data = [];
                        data.push(n.id);
                        if(nodes.length > 1){
                            recursion.cancelNode(categoryType.category_zTree, n);
                        }
                        return;
                    }
                    //names.push(n.name);
                    data.push(n.id);
                }
                //解析参数
                var rsCategory = [];
                if(data != null && data.length > 0){
                    for(var i = 0; i < data.length; i++){
                        rsCategory.push(recursion.getAllChildren(categoryType.category_zTree, data[i]));
                    }
                }
                $("#categoryId").val(rsCategory.join(","));
               // $("#company_category").val(names);
                $("#tree_category>li>span.chk").hide();// 隐藏root节点checkbox
            }
        }
      },
      zNodes: '',
      rMenu: ''
};

//初始化树
categoryType.initTree = function(){
  $.ajax({
    url: categoryType.tree_options.url,
    async: false,
    type: 'post',
    data: {},
    success: function(data){
      categoryType.tree_options.zNodes = eval(data.result);
      // 加载树
      categoryType.category_zTree = $.fn.zTree.init($("#tree_category"), categoryType.tree_options.setting, categoryType.tree_options.zNodes);
      $("#tree_category>li>span.chk").hide();// 隐藏root节点checkbox
    }
  });
};

/**
 * 行政区域树
 * @type {Object}
 */
var regionTree = new Object();

regionTree.init = function () {
    regionTree.initTree();
}

regionTree.tree_options = {
    id: 'tree_region',
    setting:{
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType :  { "Y" : "", "N" : "" }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            dblClickExpand: false
        },
        callback: {
            onCheck: function (event, treeId, treeNode) {
                var nodes = regionTree.region_zTree.getCheckedNodes(true);
                var data = []//,names = [];
                for(var i = 0; i < nodes.length; i++){
                    var n = nodes[i];
                    if(n.pId == null){
                        data = [];
                        data.push(n.id);
                        if(nodes.length > 1){
                            recursion.cancelNode(regionTree.region_zTree, n);
                        }
                        return;
                    }
                   // names.push(n.name);
                    data.push(n.id);
                }
                //解析参数
                var rsRegion = [];
                if(data != null && data.length > 0){
                    for(var i = 0; i < data.length; i++){
                        rsRegion.push(recursion.getAllChildren(regionTree.region_zTree, data[i]));
                    }
                }
                $("#regionId").val(rsRegion.join(","));
                //$("#company_region").val(names);
                $("#tree_region>li>span.chk").hide();// 隐藏root节点checkbox
            }
        }
    }
}

regionTree.initTree = function () {
    // 加载树
    resion_tree[0]["open"] = false;
    resion_tree[0]["check"] = false;
    regionTree.region_zTree = $.fn.zTree.init($("#tree_region"), regionTree.tree_options.setting, resion_tree);
    $("#tree_region>li>span.chk").hide();// 隐藏root节点checkbox
};


// 递归获取所有子节点
var recursion = new Object();
recursion.getAllChildren = function (tree, id) {
    var nodes = tree.getNodesByParam("id", id, null);
    var array = [];
    if(nodes.length > 0 && nodes[0].isParent){
        var childes = nodes[0].children;
        if(childes.length > 0){
            for(var i = 0; i < childes.length; i++){
                array.push(recursion.getAllChildren(tree, childes[i].id));
            }
        }
    }
    array.push(nodes[0].id);
    return array;
}

recursion.cancelNode = function (treeObj, node) {
    treeObj.checkAllNodes(false);
    treeObj.checkNode(node, true, true);
};


//搜索高亮
function getFontCss(treeId, treeNode){
    return (!!treeNode.highlight)?{'color': "#A60000", 'font-weight':"bold"}:{'color': "#333", 'font-weight':"normal"};
}